﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using FluffyUnderware.Curvy;
using NodeCanvas.Framework;
using FluffyUnderware.Curvy.Controllers;

namespace Yoozoo.Gameplay.City.AI
{
    public class CityNPCBirthManager : MonoBehaviour
    {
		private Dictionary<string, List<CityAIEntity>> moduleMap;

		private void Awake()
		{
			moduleMap = new Dictionary<string, List<CityAIEntity>>();
		}
		/// <summary>
		/// 内城内行走的npc
		/// </summary>
		/// <param name="model"></param>
		/// <param name="graph"></param>
		/// <param name="spline"></param>
		/// <returns></returns>
		public bool AddNpc(GameObject model, GameObject graph , CurvySpline spline)
        {
			CityAIEntity aiEntity = model.AddComponent<CityAIEntity>();
			NPCCustomPath npcCustom = model.AddComponent<NPCCustomPath>();
			aiEntity.transform.parent = this.transform;

			aiEntity.name = model.name;

			// Controller （状态机、行为树）
			GraphOwner graphOwner = graph.GetComponent<GraphOwner>();
			Blackboard blackboard = graph.GetComponent<Blackboard>();
			//blackboard.SetVariableValue("Name", model.name);
			blackboard.SetVariableValue("CityAIEntity", aiEntity);
			aiEntity.graph = graphOwner;
			aiEntity.blackboard = blackboard;
			graph.transform.SetParent(aiEntity.transform);

			// Controller（寻路组件）
			var splineController = aiEntity.gameObject.AddComponent<SplineController>();
			aiEntity.splineController = splineController;
			aiEntity.splineController.Spline = spline;
			aiEntity.splineController.OnControlPointReached.AddListener(OnCpReached);

			//随机路线
			aiEntity.splineController.ConnectionBehavior = SplineControllerConnectionBehavior.Custom;
			aiEntity.splineController.ConnectionCustomSelector = npcCustom;
			//aiEntity.splineController.Speed = 1;
			aiEntity.splineController.Clamping = CurvyClamping.PingPong;
			// View
			Animator animator = model.GetComponent<Animator>();
			animator.updateMode = AnimatorUpdateMode.AnimatePhysics;
			animator.cullingMode = AnimatorCullingMode.CullCompletely;

			model.transform.SetParent(aiEntity.transform);
			model.transform.localPosition = Vector3.zero;
			model.transform.rotation = Quaternion.identity;

			if (spline == null)
			{
				aiEntity.gameObject.SetActive(false);
			}
			aiEntity.viewer = new AIViewer()
			{
				transform = aiEntity.transform,
				animator = animator,
			};

			// Data
			
			return true;
        }

		/// <summary>
		/// 功能建筑物内的表现npc
		/// </summary>
		/// <param name="model"></param>
		/// <param name="graph"></param>
		/// <returns></returns>
		public bool AddModuleNpc(GameObject model, GameObject graph)
		{
			CityAIEntity aiEntity = model.AddComponent<CityAIEntity>();
			
			aiEntity.transform.parent = this.transform;

			aiEntity.name = model.name;

			// Controller （状态机、行为树）
			GraphOwner graphOwner = graph.GetComponent<GraphOwner>();
			Blackboard blackboard = graph.GetComponent<Blackboard>();
			//graphOwner.graph = fsm;
			//blackboard.SetVariableValue("Name", model.name);
			blackboard.SetVariableValue("CityAIEntity", aiEntity);
			aiEntity.graph = graphOwner;
			aiEntity.blackboard = blackboard;
			graph.transform.SetParent(aiEntity.transform);

			// View
			//功能建筑内的小人父节点上的动画机为位移动画机
			//子节点上的动画机为表现动画
			//最多有两个动画机
			Animator moveAnimator = model.GetComponent<Animator>();
			Animator animator = model.GetComponentInChildren<Animator>();
			//moveAnimator.updateMode = AnimatorUpdateMode.AnimatePhysics;

			animator.cullingMode = AnimatorCullingMode.CullCompletely;
			
			model.transform.SetParent(aiEntity.transform);
			model.transform.localPosition = Vector3.zero;
			model.transform.rotation = Quaternion.identity;

			aiEntity.viewer = new AIViewer()
			{
				transform = aiEntity.transform,
				moveAnimator = moveAnimator,
				animator = animator,
			};

			if (!moduleMap.ContainsKey(model.name))
			{
				moduleMap[model.name] = new List<CityAIEntity>();
			}
			moduleMap[model.name].Add(aiEntity);

			return true;
		}

		public void SetModuleModelState(string modelName , int state)
		{
			if (moduleMap.ContainsKey(modelName))
			{
				foreach (var entity in moduleMap[modelName])
				{
					entity.blackboard.SetVariableValue("state", state);
				}
			}
		}

		public void OnCpReached(CurvySplineMoveEventArgs args)
		{
			// 先注释，有GC，这个版本不用
			// CurvySplineSegment point = args.ControlPoint;
			// SplinePointTriggerBehavior triggerBehavior = point.gameObject.GetComponent<SplinePointTriggerBehavior>();
			// if (triggerBehavior != null)
			// {
			// 	SplineController controller = args.Sender;
			// 	CityAIEntity aiEntity = controller.gameObject.GetComponent<CityAIEntity>();
			// 	aiEntity.blackboard.SetVariableValue("SplinePointEvent", triggerBehavior.GetEventParam());
			// }
		}
    }
}


